﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ISS.Models;
using System.IO;

namespace ISS
{
    public class UseDAO
    {
        public int Insert(Models.Users user, Models.ISSContext context)
        {
            user.Avatar = "";
            user.BirthDay = DateTime.Now;
            user.FullName = "";
            user.IsActive = true;
            user.UserType = (byte)Users.USER_TYPE.NORMAL;
            user.Phone = "";
            user.RegisterDate = DateTime.Now;

            context.dbUsers.Add(user);

            int result = context.SaveChanges();

            //Neu them moi nguoi dung thanh cong thi tao thu muc
            if (result > 0)
            {
                try
                {
                    String path = HttpContext.Current.Server.MapPath("/Resource");
                    Directory.CreateDirectory(path + "/" + user.UserName);
                }
                catch (Exception ex)
                {
                }
            }

            //Tao album mac dinh ban dau, dat ten la Untitled.
            Album album = new Album()
            {
                AlbumAlias = "Untitled",
                AlbumName = "Untitled", CommentCount = 0, CreateDate = DateTime.Now, 
                Creator = user.UserId, EmbedHtml = "",
                ImageCount = 0, NumbersOfImage = 0, Permission = 0, Like = 0, 
                Tags = "", Thumbnail= "", ViewCount = 0
            };
            context.dbAlbum.Add(album);
            context.SaveChanges();
            return result;
        }
    }
}